package com.ylzinfo.jck.task;

import com.ylzinfo.jck.common.Const;
import com.ylzinfo.jck.dao.Ab01AMapper;
import com.ylzinfo.jck.dao.Ac01AMapper;
import com.ylzinfo.jck.dao.Ac02AMapper;
import com.ylzinfo.jck.domain.Ab01A;
import com.ylzinfo.jck.domain.Ac01A;
import com.ylzinfo.jck.domain.Ac02A;
import com.ylzinfo.jck.service.Ab01aService;
import com.ylzinfo.jck.service.Ac01aService;
import com.ylzinfo.jck.service.Ac02aService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description 基础库定时任务
 * @author sunqy
 * @date 2019/11/21
 */
@Component
public class SynchroTask {
    private Logger logger = LoggerFactory.getLogger(SynchroTask.class);
    @Resource
    private Ac01aService ac01aService;
    @Resource
    private Ac02aService ac02aService;
    @Resource
    private Ab01aService ab01aService;
    @Resource
    private Ac01AMapper ac01AMapper;
    @Resource
    private Ac02AMapper ac02AMapper;
    @Resource
    private Ab01AMapper ab01AMapper;

    /**
     * 每天执行一次，每天凌晨5点执行
     */
    @Scheduled(cron = "0 0 5 * * ?")
    public void synchroDeal() {
        logger.info("定时任务开始");

        logger.info("定时任务处理人员基本信息开始");
        // 查出ac01a 中所有未处理的数据
        List<Ac01A> ac01AList = ac01AMapper.selectUndeal();
        // 循环处理每一条记录
        if (!CollectionUtils.isEmpty(ac01AList)) {
            ac01AList.forEach(ac01A -> {
                try {
                    ac01aService.dealAc01aToAc01(ac01A);
                } catch (Exception e) {
                    logger.error("aaz001:{}", ac01A.getAaz001());
                    logger.error("{}", e);
                    // 更新标志位，处理失败
                    ac01A.setState(Const.DealStatusEnum.ERROR.getCode());
                    // 错误原因
                    ac01A.setMsg(Const.DealStatusEnum.ERROR.getMsg() + "," + e.getMessage());
                }
                ac01AMapper.updateByPrimaryKeySelective(ac01A);
            });
        }
        logger.info("定时任务处理人员基本信息结束");

        logger.info("定时任务处理人员参保信息开始");
        // 查出ac02a 中所有未处理的数据
        List<Ac02A> ac02aList = ac02AMapper.selectUndeal();
        // 循环处理每一条记录
        if (!CollectionUtils.isEmpty(ac02aList)) {
            ac02aList.forEach(ac02A -> {
                try {
                    ac02aService.dealAc02aToAc02(ac02A);
                } catch (Exception e) {
                    logger.error("aaz002:{}:{}", ac02A.getAaz002(), e);
                    // 更新标志位，处理失败
                    ac02A.setState(Const.DealStatusEnum.ERROR.getCode());
                    // 错误原因
                    ac02A.setMsg(Const.DealStatusEnum.ERROR.getMsg() + "," + e.getMessage());
                }
                ac02AMapper.updateByPrimaryKeySelective(ac02A);
            });
        }
        logger.info("定时任务处理人员参保信息结束");

        logger.info("定时任务处理单位信息开始");
        // 查出ab01a 中所有未处理的数据
        List<Ab01A> ab01AList = ab01AMapper.selectUndeal();
        // 循环处理每一条记录，这个每一条里面已经有基础库中的aac001了
        if (!CollectionUtils.isEmpty(ab01AList)) {
            ab01AList.forEach(ab01A -> {
                try {
                    ab01aService.dealAb01aToAb01(ab01A);
                } catch (Exception e) {
                    logger.error("aaz003:{},{}", ab01A.getAaz003(), e);
                    // 更新标志位，处理失败
                    ab01A.setState(Const.DealStatusEnum.ERROR.getCode());
                    // 错误原因
                    ab01A.setMsg(Const.DealStatusEnum.ERROR.getMsg() + "," + e.getMessage());
                }
                ab01AMapper.updateByPrimaryKeySelective(ab01A);
            });
        }
        logger.info("定时任务处理单位信息结束");

        logger.info("定时任务结束");
    }
}
